<template>
  <view>
    <!-- 搜索框 -->
    <view class="search-box">
      <uni-search-bar @input="input" :radius="100" cancelButton="none" placeholder="请输入搜索内容" />
    </view>
    <!-- 搜索建议列表 -->
    <view class="searchList" v-if="searchResult.length !== 0">
      <view class="suggestItem" v-for="(item,index) in searchResult" :key="index" @click='DetailGoods(item)'>
        <view class="goods-name">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    
    <!-- 搜索历史 -->
    <view class="searchHistory" v-if="searchResult.length === 0">
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="20" @click="clearStorge"></uni-icons>
      </view>
      <view class="history-list">
        <uni-tag :text="item" type="success" v-for="(item,index) in hisStoryList" :key="index" @click="gotoGoodsList(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  import {SearchSuggest} from '../../service/search_api.js'
  import myCache from '../../cache/my-cache.js'
  export default {
    data() {
      return {
        timer: null,
        keyword: null,
        searchResult: [],//搜索结果列表
        hisStoryList:[]
      };
    },
    //加载时获取搜索历史
    onLoad(){
      this.hisStoryList = JSON.parse(myCache.getItem('kw') || '[]') 
    },
    methods:{
      // search输入函数/防抖处理
      input(e){
        clearTimeout(this.timer)
        this.timer = setTimeout(()=>{
          this.keyword = e.valueOf()
          this.getSearchResult()
        },500)
      },
      
      //拿到搜索建议列表数据
      async getSearchResult(){
        if(this.keyword === ''){
          this.searchResult = []
          return 
        }
        const res = await SearchSuggest(this.keyword)
        this.searchResult = res.message  
        this.saveHistoryWord()
      },
      
      //保存搜索历史
      saveHistoryWord(){
        //数组头前添加关键词
        this.hisStoryList.unshift(this.keyword)
        //删除重复项
        const set = new Set(this.hisStoryList)
        this.hisStoryList = [...set]
        //将历史搜索存储到storge中
        myCache.setItem('kw',JSON.stringify(this.hisStoryList))
      },
      
      //清空搜索历史
      clearStorge(){
        this.hisStoryList = []
        myCache.removeItem('kw')
      },
      
      //搜索建议item跳转详情页
      DetailGoods(item){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      },
      
      //搜索历史点击跳转页
      gotoGoodsList(item){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query=' + item
        })
      }
    }
  }
</script>

<style lang="scss">
.search-box{
  position:sticky;
  top:0;
  z-index:999;
}

.searchList{
  padding:0 5px;
  
  .suggestItem{
    font-size:14px;
    padding:13px 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #efefef;
    .goods-name{
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}

.searchHistory{
  .history-title{
    display: flex;
    justify-content: space-between;
    padding:5px 5px;
    height:40px;
    line-height: 40px;
    align-items: center;
    font-size:14px;
    border-bottom: 1px solid #efefef;
  }
  .history-list{
    display: flex;
    flex-wrap: wrap;
    margin-top: 10px;
      
    .uni-tag{
      margin:0 5px;
    }
  }
}
</style>
